<style>
    .custom-tree-node{
        width: 100%;
        display: flex;
        justify-content: space-between;
        padding-right: 20px;
        align-items: center;
    }
    .tree{
        height: 100%;
        border-right: 1px solid #ececec;
    }
    .title{
        background: #1890ff;
        color: #ffffff;
        height: 34px;
        line-height: 34px;
        padding-left: 20px;
        text-align: left;
    }
</style>
<template>
    <div class="tree">
        <div class="title">菜单</div>
        <el-tree
                :data="data"
                node-key="id"
                default-expand-all
                :expand-on-click-node="false"
                @node-click="handleNodeClick">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
                  type="text"
                  size="medium"
                  icon="el-icon-circle-plus-outline"
                  @click="() => append(data)">
          </el-button>
          <el-button
                  type="text"
                  size="medium"
                  icon="el-icon-delete"
                  @click="() => remove(node, data)"
          >
          </el-button>
        </span>
      </span>
        </el-tree>
    </div>
</template>

<script>
    let id = 1000;
    export default {
        name: "tree",
        data() {
            return {
                data:[{
                    id: 1,
                    label: '一级 1',
                    children: [{
                        id: 4,
                        label: '二级 1-1',
                        children: [{
                            id: 9,
                            label: '三级 1-1-1'
                        }, {
                            id: 10,
                            label: '三级 1-1-2'
                        }]
                    }]
                }, {
                    id: 2,
                    label: '一级 2',
                    children: [{
                        id: 5,
                        label: '二级 2-1'
                    }, {
                        id: 6,
                        label: '二级 2-2'
                    }]
                }, {
                    id: 3,
                    label: '一级 3',
                    children: [{
                        id: 7,
                        label: '二级 3-1'
                    }, {
                        id: 8,
                        label: '二级 3-2'
                    }]
                }]
            }
        },
        methods:{
            handleNodeClick(data){
                //this.$EventBus.$emit('changeType',{type:'tree',id:data.id});
            },
            append(data) {
                const newChild = { id: id++, label: 'testtest', children: [] };
                if (!data.children) {
                    this.$set(data, 'children', []);
                }
                data.children.push(newChild);
            },

            remove(node, data) {
                if(node.isLeaf){
                    const parent = node.parent;
                    const children = parent.data.children || parent.data;
                    const index = children.findIndex(d => d.id === data.id);
                    children.splice(index, 1);
                }else {

                }
            }
        }
    }
</script>

